package me.green.dao;

import java.sql.*;

/**
 * 名言警句Dao
 *
 * @author green
 */
public class BywordDao {

    /**
     * 保存用户自定义名言警句（格言）
     *
     * @param uid
     * @param byword
     * @return
     */
    public boolean saveUserByword(int uid, String byword) {

        Connection conn = null;
        try {
            conn = BaseDB.getConnection();
            String deleteSql = "delete from byword where uid=? ";
            PreparedStatement statement = conn.prepareStatement(deleteSql);
            statement.setInt(1, uid);
            statement.executeUpdate();

            String sql = "insert into byword(uid,byword,update_time)values(?,?,?)";
            PreparedStatement ps = conn.prepareStatement(sql);
            ps.setInt(1, uid);
            ps.setString(2, byword);
            ps.setTimestamp(3, new Timestamp(System.currentTimeMillis()));
            int i = ps.executeUpdate();

            statement.close();
            ps.close();

            return i > 0 ? true : false;
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            BaseDB.release(conn);
        }
        return false;
    }

    /**
     * 获取用户设置的格言
     *
     * @param uid
     * @return
     */
    public String getUserByword(int uid) {
        String words = "";
        String sql = "select byword from byword where uid=? ";
        Connection conn = null;
        try {
            conn = BaseDB.getConnection();
            PreparedStatement ps = conn.prepareStatement(sql);
            ps.setInt(1, uid);
            ResultSet rs = ps.executeQuery();
            while (rs.next()) {
                words = rs.getString("byword");
            }
            rs.close();
            ps.close();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            BaseDB.release(conn);
        }
        return words;
    }


}
